/*
  main.cpp
  list_intel
  Created by user on 13.11.13.
  Copyright (c) 2013 user. All rights reserved.
*/

#include <iostream>
template <class any>
class List
{
    struct node
    {
        any Num;
        node *next,*prev;
    };
    node* new_node ()
    {
        node *l;
        l=new node;
        return l;
    }
    node* create_node(any N)
    {
        node *l=new_node();
        l->Num=N;
        l->next=l->prev=NULL;
        return l;
    }
    node *Head;
public:
    void push_front(any x);
    void push_back(any x);
    void push_middle(any x);
    any pop_front();
    any pop_back();
    int size();
    void show();
    void show_reverse();
    ~List();
};
template <class any>
void List<any>::show()
{
    node *temp=Head;
    while (temp!=NULL)
    {
        std::cout<<temp->Num<<" ";
        temp=temp->next;
    }
    std::cout<<"\n";
}
template <class any>
void List<any>::show_reverse()
{
    node *temp=Head;
    while (temp->next!=NULL) {
        temp=temp->next;
    }
    while (temp!=NULL)
    {
        std::cout<<temp->Num<<" ";
        temp=temp->prev;
    }
    std::cout<<"\n";
}
template <class any>
void List<any>::push_front(any x)
{
    node *temp=create_node(x);
    if (Head!=NULL)
    {
        temp->next=Head;
        Head->prev=temp;
        Head=temp;
    }
    else
        Head=temp;
}

template <class any>
void List<any>::push_back(any x)
{
    node *temp=create_node(x), *n=Head;
    if (Head!=NULL)
    {
        while (n->next!=NULL)
        {
            n=n->next;
        }
        n->next=temp;
        temp->prev=n;
    }
    else
        Head=temp;
        
}
template <class any>
void List<any>::push_middle(any x)  /* добавляю лист после головы*/
{
    node *temp=create_node(x), *n=Head;
    if (Head!=NULL) {
        n=n->next;
        if (n!=NULL) {
            Head->next=temp;
            temp->prev=Head;
            n->prev=temp;
            temp->next=n;
        }
        else
        {
            n=temp;
            Head->next=n;
            n->prev=Head;
        }
            
    }
    else
        Head=temp;
}
template <class any>
any List<any>::pop_back()
{
    node *n=Head;
    if (Head!=NULL)
    {
        while (n->next!=NULL)
        {
            n=n->next;
        }
        n=n->prev;
        int a=n->next->Num;
        delete(n->next);
        n->next=NULL;
        return a;
    }
    else
        return 0;
}
template <class any>
any List<any>::pop_front()
{
    node *n=Head;
    if (Head!=NULL)
    {
        int a=n->Num;
        Head=n->next;
        delete n;
        Head->prev=NULL;
        return a;
    }
    else
        return 0;
}
template <class any>
int List<any>::size()
{
    node *n=Head;
    if (Head!=NULL)
    {
        int count=0;
        while (n!=NULL) {
            count++;
            n=n->next;
        }
        return count;
    }
    else
        return 0;
}
template <class any>
List<any>::~List()
{
    node *n=Head;
    while (Head)
    {
        n=Head->next;
        delete Head;
        Head=n;
    }
}
int main(int argc, const char * argv[])
{
    //int i;
    // insert code here...
    List <char> l;
    l.push_back(51);
    l.push_middle(50);
    l.push_front(52);
    l.push_front(53);
    l.push_middle(55);
    int count=l.size();
    std::cout<<count<<' ';
    l.show();
    l.show_reverse();
    int i=l.pop_back();
    std::cout<<i<<' ';
    l.show();
    
    
   
    
    
    
    
    //std::cout << "Hello, World!\n";
    return 0;
}

